package com.yuncommunity.runcircle.list;

import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.oldfeel.base.BaseList;
import com.oldfeel.utils.DensityUtil;
import com.oldfeel.utils.NetUtil;
import com.oldfeel.utils.ViewHelper;
import com.yuncommunity.runcircle.HistoryDetail;
import com.yuncommunity.runcircle.R;
import com.yuncommunity.runcircle.conf.JsonApi;
import com.yuncommunity.runcircle.conf.Net;
import com.yuncommunity.runcircle.item.HistoryItem;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by oldfeel on 11/22/15.
 */
public class HistoryList extends BaseList<HistoryItem> {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.base_swipe_menu_list_fragment, container,
                false);
        mSwipeRefreshLayout = (SwipeRefreshLayout) view
                .findViewById(R.id.swiperefresh);
        mListView = (ListView) view.findViewById(R.id.listview);
        mListView.setBackgroundResource(R.color.white);
        return view;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        SwipeMenuListView listView = (SwipeMenuListView) mListView;
        listView.setMenuCreator(creator);
        listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:
                        // open
                        onItemClick(position);
                        break;
                    case 1:
                        delete(getItem(position));
                        // delete
                        break;
                }
                // false : close the menu; true : not close the menu
                return false;
            }
        });
        listView.setOnSwipeListener(new SwipeMenuListView.OnSwipeListener() {
            @Override
            public void onSwipeStart(int position) {
                mSwipeRefreshLayout.setEnabled(false);
            }

            @Override
            public void onSwipeEnd(int position) {
                mSwipeRefreshLayout.setEnabled(true);
            }
        });
    }

    private void delete(final HistoryItem item) {
        new AlertDialog.Builder(getActivity()).setTitle("删除后无法恢复,是否确定要删除?")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        deleteSportData(item);
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }

    private void deleteSportData(final HistoryItem item) {
        Net net = new Net(getActivity(), JsonApi.deleteSportData);
        net.setParams("id", item.id);
        net.sendPost(new NetUtil.OnJsonResultListener() {
            @Override
            public void onSuccess(String data) {
                showToast("删除成功");
                getAdapter().remove(item);
            }

            @Override
            public void onFail(int code, String data) {
                showToast("删除失败");
            }
        });
    }

    SwipeMenuCreator creator = new SwipeMenuCreator() {

        @Override
        public void create(SwipeMenu menu) {
            // create "open" item
            SwipeMenuItem openItem = new SwipeMenuItem(
                    getActivity());
            // set item background
            openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
                    0xCE)));
            // set item width
            openItem.setWidth(DensityUtil.dip2px(getActivity(), 90));
            // set item title
            openItem.setTitle("Open");
            // set item title fontsize
            openItem.setTitleSize(18);
            // set item title font color
            openItem.setTitleColor(Color.WHITE);
            // add to menu
            menu.addMenuItem(openItem);

            // create "delete" item
            SwipeMenuItem deleteItem = new SwipeMenuItem(
                    getActivity());
            // set item background
            deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
                    0x3F, 0x25)));
            // set item width
            deleteItem.setWidth(DensityUtil.dip2px(getActivity(), 90));
            // set a icon
            deleteItem.setIcon(R.drawable.ic_delete_white_24dp);
            // add to menu
            menu.addMenuItem(deleteItem);
        }
    };

    @Override
    public void onItemClick(int position) {
        openActivity(HistoryDetail.class, getItem(position));
    }

    @Override
    public void initHeaderView() {

    }

    @Override
    public View getItemView(LayoutInflater inflater, ViewGroup parent, int position) {
        View view = inflater.inflate(R.layout.item_history, parent, false);
        HistoryItem item = getItem(position);
        ViewHelper helper = new ViewHelper(view);
        helper.id(R.id.date).text(item.date);
        helper.id(R.id.distance).text(item.distance + "");
        helper.id(R.id.speed).text(item.getSpeed());
        helper.id(R.id.duration).text(item.getDuration());
        return view;
    }

    @Override
    public String parseData(String data) {
        try {
            JSONObject json = new JSONObject(data);
            return json.getString("dataList");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return super.parseData(data);
    }

    public static Fragment newInstance(Net net) {
        HistoryList list = new HistoryList();
        list.netUtil = net;
        list.itemClass = HistoryItem.class;
        list.pageKey = "p";
        list.pageStart = 1;
        return list;
    }
}
